/**
 * 我的订单
 * @author Qasim <15750783791@163.com>
 * @version 2016/10/31
 */
var self = memberInfo = ''
document.addEventListener('plusready' , ready ,  false);

function ready(){
	self = plus.webview.currentWebview(); 
	memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);
	//我的订单
	myorder();
}


function myorder(){
	$.ajax({
		type:"post",  
		url:apiRoot + "/Home/Order/myOrder/", 
		data:{
			uid :  memberInfo.aid 
		},
		beforeSend:function(){
			plus.nativeUI.showWaiting('加载中...');
		},
		complete:function(){
			plus.nativeUI.closeWaiting();
		},
		dataType:'json',
 		success : function(data){
 			if(data){
 				var html = '';
 				$.each(data , function(k ,v){
 					html += '<div class="list-p"><ul class="mui-table-view"><li class="mui-table-view-cell mui-media">';
					html += '<a href="javascript:;"><img  class="mui-media-object mui-pull-left details" data-id="'+v.id+'" data-price="'+v.price+'" src="'+getImgUrl(v.litpic , "../public/img/book.jpg")+'">';
					html += '<div class="mui-media-body"><span class="mui-h5 back details"  data-id="'+v.id+'" data-price="'+v.price+'" >'+v.book_name+'</span>';
					html += '<p class="mui-h6 detail details" data-id="'+v.id+'" data-price="'+v.price+'" >'+v.description+'</p>';
					html += '<p class="autor"><span class="mui-h6 back">'+v.author+'</span><span class="font10 mui-pull-right"><i class="mui-icon iconfont">&#xe623;</i></span></p></div></a>';
					html += '</li></ul></div>';
 				})
				$('.lists').html(html) 
				$('.details').unbind();
			    $('.details').on('click' , function(){ 
			    	bookDetails($(this).attr('data-id') , $(this).attr('data-price'),self.id)
			    });
				    
			    $('.list-p i').unbind();
			    $('.list-p i').on('click' , function(){
			    	var __this = $(this).parent().parent().parent();
			    	plus.nativeUI.confirm('温馨提示:确定要删除该记录吗?( 1条订单包含的商品都会被删除哦)' , function(e){
						if(e.index == 0){
							var id = __this.find('.details').attr('data-id');
							deleteOrder(id);
						}
					} , '电子阅读' , ['确定' , '取消']);
			    });
 				
 				
 			}else{
 				$('.lists').html('')
 				toast('暂无记录');
 			} 
 		},
 		error :function(e){
 			errorPrompt(e);	
 		}
 	})
	
}

/**
 * 删除订单记录
 * @param {Int} id
 */ 
function deleteOrder(id){
	$.ajax({   
		type:"post",   
		url:apiRoot + "/Home/Order/deleteOrder/", 
		data:{
			id :  id ,  
			uid :memberInfo.aid
		},
		beforeSend:function(){
			plus.nativeUI.showWaiting('删除中...'); 
		},
		complete:function(){
			plus.nativeUI.closeWaiting();
		}, 
 		success : function(data){
 			if(data > 0){
				myorder();				
 				toast('删除成功'); 
 			}
 		},
 		error :function(e){
 			errorPrompt(e);	
 		}
 	})
	
	
}
